<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template session, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * save or update uploaded device session for a particular user
 * 
 * @param args = {
 * 
 *  @param user_id
 *  @param device_id
 *  @param token
 *  @param until
 * 
 * }
 */
function sessions_save($args){
    
    //import required global variables 
    global $db;
    global $contex;
    
    //extract and sanitize parameters creating a variable for each
    $fields = X_param_array ($args, 'fields'); 
    $id = X_param ($args, 'id'); 
    $data['user_id'] = X_param ($args, 'user_id'); if(!$id && !$data['user_id']) return __MOOV_PARAM_INVAL_;
    $data['device_id'] = X_param ($args, 'device_id'); if(!$id && !$data['device_id']) return __MOOV_PARAM_INVAL_;
    $data['status'] = X_param ($args, 'status', 'A'); 
    $limit = X_param_array ($args, 'limit');
    $sort = X_param_array ($args, 'sort');
    
    if(!$id){
        $data['token'] =  str_pad(rand(1000, 9999),4,'0',STR_PAD_LEFT);
        $data['added_ts'] = gmdate('y-m-d h:m:s');
    }
    
    // ---------------- Operation begins here ---------------------
    $id = $db->save('sessions',$id,$data);
    return countries_list(array('id' => $id, 'fields' => $fields, 'limit' => $limit, 'sort' => $sort));
  
}

/*
 * 
 */
function sessions_list($args){
    
      //import required global variables 
    global $db;
    global $contex;

    //extract and sanitize parameters creating a variable for each
    $fields = X_param_array ($args, 'fields'); 
    $data['id'] = X_param_array ($args, 'id'); 
    $data['name'] = X_param_array ($args, 'name'); 
    $data['added_ts'] = X_param ($args, 'added_ts');
    $data['modified_ts'] = X_param ($args, 'modified_ts');
    $data['status'] = X_param_array ($args, 'status'); 
    $limit = X_param_array ($args, 'limit');
    $sort = X_param_array ($args, 'sort');
    
    
    // ---------------- Operation begins here ---------------------
    $rc = $db->get('sessions', $fields, $data, $limit, $sort);
    return out($rc);
}
